package nu.aing.P20140616;

import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import nu.aing.utils.TestUtils;

/**
 * @author memekmaut@gmail.com
 */
public class CatchTheBeatEasy {
    
    public static void main(String[] args) {
        CatchTheBeatEasy obj = new CatchTheBeatEasy();
        TestUtils.test(
                obj, "ableToCatchAll",
                new int[] {-1, 1, 0}, 
                new int[] {1, 3, 4}
        );
        TestUtils.test(
                obj, "ableToCatchAll",
                new int[] {-3}, 
                new int[] {2}
        );
        TestUtils.test(
                obj, "ableToCatchAll",
                new int[] {-1, 1, 0}, 
                new int[] {1, 2, 4}
        );
        TestUtils.test(
                obj, "ableToCatchAll",
                new int[] {0, -1, 1}, 
                new int[] {9, 1, 3}
        );
        TestUtils.test(
                obj, "ableToCatchAll",
                new int[] {70,-108,52,-70,84,-29,66,-33}, 
                new int[] {141,299,402,280,28,363,427,232}
        );
        TestUtils.test(
                obj, "ableToCatchAll",
                new int[] {-175,-28,-207,-29,-43,-183,-175,-112,-183,-31,-25,-66,-114,-116,-66}, 
                new int[] {320,107,379,72,126,445,318,255,445,62,52,184,247,245,185}
        );
        TestUtils.test(
                obj, "ableToCatchAll",
                new int[] {0,0,0,0}, 
                new int[] {0,0,0,0}
        );
    }
    
    public String ableToCatchAll(int[] x, int[] y) {
        SortedMap<Integer, Integer> c = new TreeMap<Integer, Integer>();
        for(int i = 0; i < x.length; i++) {
            c.put(y[i], x[i]);
        }
        return x(0, c) ? "TRUE" : "FALSE";
    }
    
    public boolean x(int x, SortedMap<Integer, Integer> c) {
        int min = 0; Integer key = null;
        SortedMap<Integer, Integer> cc = new TreeMap<Integer, Integer>();
        for(Entry<Integer, Integer> e : c.entrySet()) {
            if(e.getKey() < 0 && x == e.getValue()) return false;
            cc.put(e.getKey() - 1, e.getValue() );
        }
        min = cc.firstKey();
        x = cc.get(min) == x ? x : 
            cc.get(min) > x ? ++x : --x;
        key = (min == 0 && x == c.get(c.firstKey()) || 
                (x == cc.get(cc.firstKey()) && x == c.firstKey() ) ) ? 
                        key = cc.firstKey() : null;
        if(key != null) cc.remove(key);
        if(cc.isEmpty() ) return true;
        else return x(x, cc);
    }
}
